//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.controller
{
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	
	import reader.ApplicationFacade;
	import reader.core.ReaderModelLocator;
	import reader.core.ReaderResource;
	import reader.core.UserConfirmCommandConfig;
	import reader.model.vo.TitleWindowVO;

	public class RemoveSubscribeCommand extends SimpleCommand implements ICommand
	{
		//////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var titleWindow:TitleWindowVO;
		private var isArray:Boolean;
		private var resource:XML = ReaderResource.instance.resource;
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		override public function execute(notification:INotification):void
		{
			titleWindow = notification.getBody() as TitleWindowVO;
			var alertMessage:String;
			if(titleWindow.blogSource)
			{
				if(titleWindow.blogSource.length == 1)
				{
					alertMessage = resource.settingPanelManageSubMinButtonAlert + " " + titleWindow.blogSource[0].blogName + " ?"
				}
				else
				{
					alertMessage = resource.settingPanelManageSubRemoveAllButtonAlert;
					isArray = true;
				}
			}
			var cfm:UserConfirmCommandConfig = new UserConfirmCommandConfig(resource.settingPanelManageSubMinButton, alertMessage, titleWindow.parent, onUnSubscribe_handler);
			facade.sendNotification(ApplicationFacade.REQUEST_USERCONFIRM, cfm);
			/* Alert.yesLabel = readerApplication.resource.ganeralOK;
			Alert.cancelLabel = readerApplication.resource.ganeralCancel;					
			var alert:Alert = Alert.show(alertMessage,
				readerApplication.resource.settingPanelManageSubMinButton, 
				Alert.OK | Alert.CANCEL,
				titleWindow.parent, onUnSubscribe_handler);	
			//alert.width = 220;	
			alert.height = 100;
			alert.setStyle("paddingBottom", 5); */
		}
		
		private function onUnSubscribe_handler():void
		{
			if(isArray)
			{
				titleWindow.dataProxy.removeSubscribes(titleWindow.blogSource);
			}
			else
			{
				titleWindow.dataProxy.removeSubscribe(titleWindow.blogSource[0]);
				delete readerModelLocator.feedDictionary[titleWindow.blogSource[0].blogSourceId];
				sendNotification(ApplicationFacade.REMOVED_SUBSCRIBE, titleWindow.blogSource[0]);
			}
		}
		
	}
}